﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using CodeStash.Common.DataAccess.Repository;
using CodeStash.Common.DataAccess.EntityFramework;

namespace CodeStash.IntegrationTests
{
    [TestFixture]
    public class OwnedTeamTests
    {
        [Test]
        public void SimpleInsertAndDeleteTest()
        {
            Repository<OwnedTeam> ownedTeamRepository = new Repository<OwnedTeam>();
            using (var db = new CodeStashEntities("name=CodeStashEntities"))
            {
                ownedTeamRepository.EnrolInUnitOfWork(db);
                Guid uniqueId = Guid.NewGuid();
                ownedTeamRepository.Add(new OwnedTeam(uniqueId,uniqueId.ToString()));
                db.SaveChanges();

                IEnumerable<OwnedTeam> teams= ownedTeamRepository.FindBy(x => x.TeamDescription == uniqueId.ToString());
                Assert.AreEqual(1,teams.Count());

                ownedTeamRepository.Remove(teams.First());
                db.SaveChanges();

                teams = ownedTeamRepository.FindBy(x => x.TeamDescription == uniqueId.ToString());
                Assert.AreEqual(0, teams.Count());
            }
        }


        [Test]
        public void SimpleUpdateTest()
        {
            Repository<OwnedTeam> ownedTeamRepository = new Repository<OwnedTeam>();
            using (var db = new CodeStashEntities("name=CodeStashEntities"))
            {
                ownedTeamRepository.EnrolInUnitOfWork(db);
                Guid uniqueId = Guid.NewGuid();
                ownedTeamRepository.Add(new OwnedTeam(uniqueId, uniqueId.ToString()));
                db.SaveChanges();

                IEnumerable<OwnedTeam> teams = ownedTeamRepository.FindBy(x => x.TeamDescription == uniqueId.ToString());
                Assert.AreEqual(1, teams.Count());

                Guid uniqueId2 = Guid.NewGuid();
                OwnedTeam addedTeam = teams.First();
                addedTeam.TeamDescription = uniqueId2.ToString();
                addedTeam.TeamOwnerAspMembershipUserId = uniqueId2;
                db.SaveChanges();

                int id = addedTeam.TeamId;

                teams = ownedTeamRepository.FindBy(x => x.TeamId == id);
                OwnedTeam updatedTeam = teams.First();
                Assert.AreEqual(1, teams.Count());
                Assert.AreEqual(uniqueId2.ToString(), updatedTeam.TeamDescription);
                Assert.AreEqual(uniqueId2, updatedTeam.TeamOwnerAspMembershipUserId);


                ownedTeamRepository.Remove(teams.First());
                db.SaveChanges();

                teams = ownedTeamRepository.FindBy(x => x.TeamId == id);
                Assert.AreEqual(0, teams.Count());
            }
        }

    }
}
